<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>6 Modifying inputs | Tidy evaluation</title>
  <meta name="description" content="这本书的目的是让你跟上 tidy evaluation 的进度，以及如何围绕 tidyverse 流程和语法编写函数" />
  <meta name="generator" content="bookdown 0.11 and GitBook 2.6.7" />

  <meta property="og:title" content="6 Modifying inputs | Tidy evaluation" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="这本书的目的是让你跟上 tidy evaluation 的进度，以及如何围绕 tidyverse 流程和语法编写函数" />
  <meta name="github-repo" content="ShixiangWang/tidyeval-chinese" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="6 Modifying inputs | Tidy evaluation" />
  
  <meta name="twitter:description" content="这本书的目的是让你跟上 tidy evaluation 的进度，以及如何围绕 tidyverse 流程和语法编写函数" />
  

<meta name="author" content="Lionel Henry" />
<meta name="author" content="Hadley Wickham" />
<meta name="author" content="王诗翔（译）" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="multiple.html">
<link rel="next" href="glossary.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />







<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-115082821-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-115082821-1');
</script>


<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="adv-r.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><strong><a href="./">Tidy evaluation</a></strong></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>欢迎</a><ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#e585b6e4bb96e8b584e6ba90"><i class="fa fa-check"></i>其他资源</a></li>
</ul></li>
<li class="part"><span><b>I 原理</b></span></li>
<li class="chapter" data-level="1" data-path="section-1.html"><a href="section-1.html"><i class="fa fa-check"></i><b>1</b> 介绍</a></li>
<li class="chapter" data-level="2" data-path="sec-why-how.html"><a href="sec-why-how.html"><i class="fa fa-check"></i><b>2</b> 为什么使用以及怎么使用</a><ul>
<li class="chapter" data-level="2.1" data-path="sec-why-how.html"><a href="sec-why-how.html#section-2.1"><i class="fa fa-check"></i><b>2.1</b> 数据隐藏</a></li>
<li class="chapter" data-level="2.2" data-path="sec-why-how.html"><a href="sec-why-how.html#section-2.2"><i class="fa fa-check"></i><b>2.2</b> 引用代码</a></li>
<li class="chapter" data-level="2.3" data-path="sec-why-how.html"><a href="sec-why-how.html#section-2.3"><i class="fa fa-check"></i><b>2.3</b> 去引用代码</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="sec-do-you-need.html"><a href="sec-do-you-need.html"><i class="fa fa-check"></i><b>3</b> 你需要 tidy eval 吗？</a><ul>
<li class="chapter" data-level="3.1" data-path="sec-do-you-need.html"><a href="sec-do-you-need.html#sec:fixed-colnames"><i class="fa fa-check"></i><b>3.1</b> 固定列名</a></li>
<li class="chapter" data-level="3.2" data-path="sec-do-you-need.html"><a href="sec-do-you-need.html#sec:automating-loops"><i class="fa fa-check"></i><b>3.2</b> 自动循环</a><ul>
<li class="chapter" data-level="3.2.1" data-path="sec-do-you-need.html"><a href="sec-do-you-need.html#dplyr-"><i class="fa fa-check"></i><b>3.2.1</b> dplyr 中的向量化</a></li>
<li class="chapter" data-level="3.2.2" data-path="sec-do-you-need.html"><a href="sec-do-you-need.html#section-3.2.2"><i class="fa fa-check"></i><b>3.2.2</b> 按列循环</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html"><i class="fa fa-check"></i><b>4</b> 加速</a><ul>
<li class="chapter" data-level="4.1" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.1"><i class="fa fa-check"></i><b>4.1</b> 编写函数</a><ul>
<li class="chapter" data-level="4.1.1" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.1.1"><i class="fa fa-check"></i><b>4.1.1</b> 减少重复</a></li>
<li class="chapter" data-level="4.1.2" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.1.2"><i class="fa fa-check"></i><b>4.1.2</b> 引用函数的特殊之处在哪？</a></li>
<li class="chapter" data-level="4.1.3" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.1.3"><i class="fa fa-check"></i><b>4.1.3</b> 去引用</a></li>
<li class="chapter" data-level="4.1.4" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#-qq_show-"><i class="fa fa-check"></i><b>4.1.4</b> 通过 <code>qq_show()</code> 理解 <code>!!</code></a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.2"><i class="fa fa-check"></i><b>4.2</b> 引用和去引用</a><ul>
<li class="chapter" data-level="4.2.1" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.2.1"><i class="fa fa-check"></i><b>4.2.1</b> 第一步：抽取</a></li>
<li class="chapter" data-level="4.2.2" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.2.2"><i class="fa fa-check"></i><b>4.2.2</b> 第二步：引用</a></li>
<li class="chapter" data-level="4.2.3" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.2.3"><i class="fa fa-check"></i><b>4.2.3</b> 第三步：去引用</a></li>
<li class="chapter" data-level="4.2.4" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.2.4"><i class="fa fa-check"></i><b>4.2.4</b> 结果</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.3"><i class="fa fa-check"></i><b>4.3</b> 用字符串取代引用</a><ul>
<li class="chapter" data-level="4.3.1" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.3.1"><i class="fa fa-check"></i><b>4.3.1</b> 字符串</a></li>
<li class="chapter" data-level="4.3.2" data-path="sec-up-to-speed.html"><a href="sec-up-to-speed.html#section-4.3.2"><i class="fa fa-check"></i><b>4.3.2</b> 包含列名的字符串向量</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="multiple.html"><a href="multiple.html"><i class="fa fa-check"></i><b>5</b> Dealing with multiple arguments</a><ul>
<li class="chapter" data-level="5.1" data-path="multiple.html"><a href="multiple.html#the-...-argument"><i class="fa fa-check"></i><b>5.1</b> The <code>...</code> argument</a></li>
<li class="chapter" data-level="5.2" data-path="multiple.html"><a href="multiple.html#simple-forwarding-of-..."><i class="fa fa-check"></i><b>5.2</b> Simple forwarding of <code>...</code></a></li>
<li class="chapter" data-level="5.3" data-path="multiple.html"><a href="multiple.html#quote-multiple-arguments"><i class="fa fa-check"></i><b>5.3</b> Quote multiple arguments</a></li>
<li class="chapter" data-level="5.4" data-path="multiple.html"><a href="multiple.html#unquote-multiple-arguments"><i class="fa fa-check"></i><b>5.4</b> Unquote multiple arguments</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="modifying-inputs.html"><a href="modifying-inputs.html"><i class="fa fa-check"></i><b>6</b> Modifying inputs</a><ul>
<li class="chapter" data-level="6.1" data-path="modifying-inputs.html"><a href="modifying-inputs.html#modifying-names"><i class="fa fa-check"></i><b>6.1</b> Modifying names</a><ul>
<li class="chapter" data-level="6.1.1" data-path="modifying-inputs.html"><a href="modifying-inputs.html#default-argument-names"><i class="fa fa-check"></i><b>6.1.1</b> Default argument names</a></li>
<li class="chapter" data-level="6.1.2" data-path="modifying-inputs.html"><a href="modifying-inputs.html#unquoting-argument-names"><i class="fa fa-check"></i><b>6.1.2</b> Unquoting argument names</a></li>
<li class="chapter" data-level="6.1.3" data-path="modifying-inputs.html"><a href="modifying-inputs.html#prefixing-quoted-arguments"><i class="fa fa-check"></i><b>6.1.3</b> Prefixing quoted arguments</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="modifying-inputs.html"><a href="modifying-inputs.html#modifying-quoted-expressions"><i class="fa fa-check"></i><b>6.2</b> Modifying quoted expressions</a><ul>
<li class="chapter" data-level="6.2.1" data-path="modifying-inputs.html"><a href="modifying-inputs.html#expanding-quoted-expressions-with-expr"><i class="fa fa-check"></i><b>6.2.1</b> Expanding quoted expressions with <code>expr()</code></a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="glossary.html"><a href="glossary.html"><i class="fa fa-check"></i><b>7</b> Glossary</a><ul>
<li class="chapter" data-level="7.1" data-path="glossary.html"><a href="glossary.html#data-structures"><i class="fa fa-check"></i><b>7.1</b> Data structures</a><ul>
<li class="chapter" data-level="7.1.1" data-path="glossary.html"><a href="glossary.html#glossary-data-mask"><i class="fa fa-check"></i><b>7.1.1</b> TODO Data mask</a></li>
<li class="chapter" data-level="7.1.2" data-path="glossary.html"><a href="glossary.html#glossary-expr"><i class="fa fa-check"></i><b>7.1.2</b> Expression</a></li>
<li class="chapter" data-level="7.1.3" data-path="glossary.html"><a href="glossary.html#expression-base"><i class="fa fa-check"></i><b>7.1.3</b> Expression (base)</a></li>
<li class="chapter" data-level="7.1.4" data-path="glossary.html"><a href="glossary.html#glossary-sym"><i class="fa fa-check"></i><b>7.1.4</b> TODO Symbol</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="glossary.html"><a href="glossary.html#programming-concepts"><i class="fa fa-check"></i><b>7.2</b> Programming Concepts</a><ul>
<li class="chapter" data-level="7.2.1" data-path="glossary.html"><a href="glossary.html#glossary-constant-symbolic"><i class="fa fa-check"></i><b>7.2.1</b> Constant versus symbolic</a></li>
<li class="chapter" data-level="7.2.2" data-path="glossary.html"><a href="glossary.html#glossary-nse"><i class="fa fa-check"></i><b>7.2.2</b> TODO Non-Standard Evaluation (NSE)</a></li>
<li class="chapter" data-level="7.2.3" data-path="glossary.html"><a href="glossary.html#glossary-quotation-evaluation"><i class="fa fa-check"></i><b>7.2.3</b> TODO Quotation versus Evaluation</a></li>
<li class="chapter" data-level="7.2.4" data-path="glossary.html"><a href="glossary.html#glossary-qq"><i class="fa fa-check"></i><b>7.2.4</b> TODO Quasiquotation</a></li>
<li class="chapter" data-level="7.2.5" data-path="glossary.html"><a href="glossary.html#glossary-parse"><i class="fa fa-check"></i><b>7.2.5</b> TODO Parsing</a></li>
<li class="chapter" data-level="7.2.6" data-path="glossary.html"><a href="glossary.html#glossary-metaprogramming"><i class="fa fa-check"></i><b>7.2.6</b> TODO Metaprogramming</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II Cookbooks</b></span></li>
<li class="chapter" data-level="8" data-path="dplyr.html"><a href="dplyr.html"><i class="fa fa-check"></i><b>8</b> dplyr</a><ul>
<li class="chapter" data-level="8.1" data-path="dplyr.html"><a href="dplyr.html#patterns-for-single-arguments"><i class="fa fa-check"></i><b>8.1</b> Patterns for single arguments</a><ul>
<li class="chapter" data-level="8.1.1" data-path="dplyr.html"><a href="dplyr.html#enquo-and---quote-and-unquote-arguments"><i class="fa fa-check"></i><b>8.1.1</b> <code>enquo()</code> and <code>!!</code> - Quote and unquote arguments</a></li>
<li class="chapter" data-level="8.1.2" data-path="dplyr.html"><a href="dplyr.html#quo_name---create-default-column-names"><i class="fa fa-check"></i><b>8.1.2</b> <code>quo_name()</code> - Create default column names</a></li>
<li class="chapter" data-level="8.1.3" data-path="dplyr.html"><a href="dplyr.html#and---unquote-column-names"><i class="fa fa-check"></i><b>8.1.3</b> <code>:=</code> and <code>!!</code> - Unquote column names</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="dplyr.html"><a href="dplyr.html#patterns-for-multiple-arguments"><i class="fa fa-check"></i><b>8.2</b> Patterns for multiple arguments</a><ul>
<li class="chapter" data-level="8.2.1" data-path="dplyr.html"><a href="dplyr.html#forward-multiple-arguments"><i class="fa fa-check"></i><b>8.2.1</b> <code>...</code> - Forward multiple arguments</a></li>
<li class="chapter" data-level="8.2.2" data-path="dplyr.html"><a href="dplyr.html#enquos-and---quote-and-unquote-multiple-arguments"><i class="fa fa-check"></i><b>8.2.2</b> <code>enquos()</code> and <code>!!!</code> - Quote and unquote multiple arguments</a></li>
<li class="chapter" data-level="8.2.3" data-path="dplyr.html"><a href="dplyr.html#expr---modify-quoted-arguments"><i class="fa fa-check"></i><b>8.2.3</b> <code>expr()</code> - Modify quoted arguments</a></li>
<li class="chapter" data-level="8.2.4" data-path="dplyr.html"><a href="dplyr.html#sec:external-quoting"><i class="fa fa-check"></i><b>8.2.4</b> <code>vars()</code> - Quote multiple arguments externally</a></li>
<li class="chapter" data-level="8.2.5" data-path="dplyr.html"><a href="dplyr.html#enquos.named-true---automatically-add-default-names"><i class="fa fa-check"></i><b>8.2.5</b> <code>enquos(.named = TRUE)</code> - Automatically add default names</a></li>
<li class="chapter" data-level="8.2.6" data-path="dplyr.html"><a href="dplyr.html#quos_auto_name---manually-add-default-names"><i class="fa fa-check"></i><b>8.2.6</b> <code>quos_auto_name()</code> - Manually add default names</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="dplyr.html"><a href="dplyr.html#select"><i class="fa fa-check"></i><b>8.3</b> <code>select()</code></a></li>
<li class="chapter" data-level="8.4" data-path="dplyr.html"><a href="dplyr.html#filter"><i class="fa fa-check"></i><b>8.4</b> <code>filter()</code></a></li>
<li class="chapter" data-level="8.5" data-path="dplyr.html"><a href="dplyr.html#case_when"><i class="fa fa-check"></i><b>8.5</b> <code>case_when()</code></a></li>
<li class="chapter" data-level="8.6" data-path="dplyr.html"><a href="dplyr.html#gotchas"><i class="fa fa-check"></i><b>8.6</b> Gotchas</a><ul>
<li class="chapter" data-level="8.6.1" data-path="dplyr.html"><a href="dplyr.html#nested-quoting-functions"><i class="fa fa-check"></i><b>8.6.1</b> Nested quoting functions</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="9" data-path="ggplot2.html"><a href="ggplot2.html"><i class="fa fa-check"></i><b>9</b> ggplot2</a></li>
<li class="part"><span><b>III Going further</b></span></li>
<li class="chapter" data-level="10" data-path="a-rich-toolbox.html"><a href="a-rich-toolbox.html"><i class="fa fa-check"></i><b>10</b> A rich toolbox</a><ul>
<li class="chapter" data-level="10.1" data-path="a-rich-toolbox.html"><a href="a-rich-toolbox.html#todo-quote-expr-and-enexpr"><i class="fa fa-check"></i><b>10.1</b> TODO <code>quote()</code>, <code>expr()</code> and <code>enexpr()</code></a></li>
<li class="chapter" data-level="10.2" data-path="a-rich-toolbox.html"><a href="a-rich-toolbox.html#todo-quo-and-enquo"><i class="fa fa-check"></i><b>10.2</b> TODO <code>quo()</code> and <code>enquo()</code></a></li>
<li class="chapter" data-level="10.3" data-path="a-rich-toolbox.html"><a href="a-rich-toolbox.html#todo-vars-quos-and-enquos"><i class="fa fa-check"></i><b>10.3</b> TODO <code>vars()</code>, <code>quos()</code> and <code>enquos()</code></a></li>
<li class="chapter" data-level="10.4" data-path="a-rich-toolbox.html"><a href="a-rich-toolbox.html#toolbox-qq-show"><i class="fa fa-check"></i><b>10.4</b> TODO <code>qq_show()</code></a></li>
<li class="chapter" data-level="10.5" data-path="a-rich-toolbox.html"><a href="a-rich-toolbox.html#todo-sym-and-syms"><i class="fa fa-check"></i><b>10.5</b> TODO <code>sym()</code> and <code>syms()</code></a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="creating-grammars.html"><a href="creating-grammars.html"><i class="fa fa-check"></i><b>11</b> Creating grammars</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Tidy evaluation</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="modifying-inputs" class="section level1">
<h1><span class="header-section-number">6</span> Modifying inputs</h1>
<p>With the quote-and-unquote pattern, quoted arguments are passed to other functions as is. In many cases you’ll find this to be too restrictive. This chapter will guide you through the steps required to pass custom argument names and custom quoted expressions.</p>
<div id="modifying-names" class="section level2">
<h2><span class="header-section-number">6.1</span> Modifying names</h2>
<p>When your function creates new columns in a data frame it’s often a good idea to give them names that reflect the meaning of those columns. In this section you’ll learn how to:</p>
<ul>
<li>Create default names for quoted arguments.</li>
<li>Unquote names.</li>
</ul>
<div id="default-argument-names" class="section level3">
<h3><span class="header-section-number">6.1.1</span> Default argument names</h3>
<p>If you are familiar with dplyr you have probably noticed that new columns are given default names when you don’t supply one explictly to <code>mutate()</code> or <code>summarise()</code>. These default names are not practical for further manipulation but they are helpful to remind rushed users what their new column is about:</p>
<pre class="sourceCode r"><code class="sourceCode r">starwars <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">summarise</span>(<span class="dt">average =</span> <span class="kw">mean</span>(height, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>))
<span class="co">#&gt; # A tibble: 1 x 1</span>
<span class="co">#&gt;   average</span>
<span class="co">#&gt;     &lt;dbl&gt;</span>
<span class="co">#&gt; 1    174.</span>

starwars <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">summarise</span>(<span class="kw">mean</span>(height, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>))
<span class="co">#&gt; # A tibble: 1 x 1</span>
<span class="co">#&gt;   `mean(height, na.rm = TRUE)`</span>
<span class="co">#&gt;                          &lt;dbl&gt;</span>
<span class="co">#&gt; 1                         174.</span></code></pre>
<p>You can create default names by applying <code>quo_name()</code> to any expressions:</p>
<pre class="sourceCode r"><code class="sourceCode r">var1 &lt;-<span class="st"> </span><span class="kw">quote</span>(height)
var2 &lt;-<span class="st"> </span><span class="kw">quote</span>(<span class="kw">mean</span>(height))

<span class="kw">quo_name</span>(var1)
<span class="co">#&gt; [1] &quot;height&quot;</span>
<span class="kw">quo_name</span>(var2)
<span class="co">#&gt; [1] &quot;mean(height)&quot;</span></code></pre>
<p>Including automatically quoted arguments:</p>
<pre class="sourceCode r"><code class="sourceCode r">arg_name &lt;-<span class="st"> </span><span class="cf">function</span>(var) {
  var &lt;-<span class="st"> </span><span class="kw">enquo</span>(var)

  <span class="kw">quo_name</span>(var)
}

<span class="kw">arg_name</span>(height)
<span class="co">#&gt; [1] &quot;height&quot;</span>

<span class="kw">arg_name</span>(<span class="kw">mean</span>(height))
<span class="co">#&gt; [1] &quot;mean(height)&quot;</span></code></pre>
<p>Lists of quoted expressions require a different approach because we don’t want to override user-supplied names. The easiest way is to call <code>enquos()</code> with <code>.named = TRUE</code>. With this option, all unnamed arguments get a default name:</p>
<pre class="sourceCode r"><code class="sourceCode r">args_names &lt;-<span class="st"> </span><span class="cf">function</span>(...) {
  vars &lt;-<span class="st"> </span><span class="kw">enquos</span>(..., <span class="dt">.named =</span> <span class="ot">TRUE</span>)
  <span class="kw">names</span>(vars)
}

<span class="kw">args_names</span>(<span class="kw">mean</span>(height), weight)
<span class="co">#&gt; [1] &quot;mean(height)&quot; &quot;weight&quot;</span>

<span class="kw">args_names</span>(<span class="dt">avg =</span> <span class="kw">mean</span>(height), weight)
<span class="co">#&gt; [1] &quot;avg&quot;    &quot;weight&quot;</span></code></pre>
</div>
<div id="unquoting-argument-names" class="section level3">
<h3><span class="header-section-number">6.1.2</span> Unquoting argument names</h3>
<p>Argument names are one of the most common occurrence of quotation in R. There is no fundamental difference between these two ways of creating a <code>&quot;myname&quot;</code> string:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(<span class="kw">c</span>(<span class="dt">Mickey =</span> <span class="ot">NA</span>))
<span class="co">#&gt; [1] &quot;Mickey&quot;</span>

<span class="kw">quo_name</span>(<span class="kw">quote</span>(Mickey))
<span class="co">#&gt; [1] &quot;Mickey&quot;</span></code></pre>
<p>Where there is quotation it is natural to have unquotation. For this reason, tidy eval makes it possible to use <code>!!</code> to unquote names. Unfortunately we’ll have to use a somewhat peculiar syntax to unquote names because using complex expressions on the left-hand side of <code>=</code> is not valid R code:</p>
<pre class="sourceCode r"><code class="sourceCode r">nm &lt;-<span class="st"> &quot;Mickey&quot;</span>
<span class="kw">args_names</span>(<span class="op">!!</span><span class="dt">nm =</span> <span class="dv">1</span>)
<span class="co">#&gt; Error: &lt;text&gt;:2:17: 意外的&#39;=&#39;</span>
<span class="co">#&gt; 1: nm &lt;- &quot;Mickey&quot;</span>
<span class="co">#&gt; 2: args_names(!!nm =</span>
<span class="co">#&gt;                    ^</span></code></pre>
<p>Instead you’ll have to unquote of the LHS of <code>:=</code>. This vestigial operator is interpreted by tidy eval functions in exactly the same way as <code>=</code> but with <code>!!</code> support:</p>
<pre class="sourceCode r"><code class="sourceCode r">nm &lt;-<span class="st"> &quot;Mickey&quot;</span>
<span class="kw">args_names</span>(<span class="op">!!</span>nm <span class="op">:</span><span class="er">=</span><span class="st"> </span><span class="dv">1</span>)
<span class="co">#&gt; [1] &quot;Mickey&quot;</span></code></pre>
<p>Another way of achieving the same result is to splice a named list of arguments:</p>
<pre class="sourceCode r"><code class="sourceCode r">args &lt;-<span class="st"> </span><span class="kw">setNames</span>(<span class="kw">list</span>(<span class="dv">1</span>), nm)
<span class="kw">args_names</span>(<span class="op">!!!</span>args)
<span class="co">#&gt; [1] &quot;Mickey&quot;</span></code></pre>
<p>This works because <code>!!!</code> uses the names of the list as argument names. This is a great pattern when you are dealing with multiple arguments:</p>
<pre class="sourceCode r"><code class="sourceCode r">nms &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;Mickey&quot;</span>, <span class="st">&quot;Minnie&quot;</span>)
args &lt;-<span class="st"> </span><span class="kw">setNames</span>(<span class="kw">list</span>(<span class="dv">1</span>, <span class="dv">2</span>), nms)
<span class="kw">args_names</span>(<span class="op">!!!</span>args)
<span class="co">#&gt; [1] &quot;Mickey&quot; &quot;Minnie&quot;</span></code></pre>
</div>
<div id="prefixing-quoted-arguments" class="section level3">
<h3><span class="header-section-number">6.1.3</span> Prefixing quoted arguments</h3>
<p>Now that we know how to unquote argument, let’s apply informative prefixes to the names of the columns created in <code>grouped_mean()</code>. We’ll start with the summary variable:</p>
<ol style="list-style-type: decimal">
<li>Get the default name of the quoted summary variable.</li>
<li>Prepend it with a prefix.</li>
<li>Unquote it with <code>!!</code> and <code>:=</code>.</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">grouped_mean2 &lt;-<span class="st"> </span><span class="cf">function</span>(.data, .summary_var, ...) {
  summary_var &lt;-<span class="st"> </span><span class="kw">enquo</span>(.summary_var)
  group_vars &lt;-<span class="st"> </span><span class="kw">enquos</span>(...)

  <span class="co"># Get and modify the default name</span>
  summary_nm &lt;-<span class="st"> </span><span class="kw">quo_name</span>(summary_var)
  summary_nm &lt;-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">&quot;avg_&quot;</span>, summary_nm)

  .data <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">group_by</span>(<span class="op">!!!</span>group_vars) <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">summarise</span>(<span class="op">!!</span>summary_nm <span class="op">:</span><span class="er">=</span><span class="st"> </span><span class="kw">mean</span>(<span class="op">!!</span>summary_var))  <span class="co"># Unquote the name</span>
}

<span class="kw">grouped_mean2</span>(mtcars, disp, cyl, am)
<span class="co">#&gt; # A tibble: 6 x 3</span>
<span class="co">#&gt; # Groups:   cyl [3]</span>
<span class="co">#&gt;     cyl    am avg_disp</span>
<span class="co">#&gt;   &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;</span>
<span class="co">#&gt; 1     4     0    136. </span>
<span class="co">#&gt; 2     4     1     93.6</span>
<span class="co">#&gt; 3     6     0    205. </span>
<span class="co">#&gt; 4     6     1    155  </span>
<span class="co">#&gt; 5     8     0    358. </span>
<span class="co">#&gt; # … with 1 more row</span>

<span class="kw">names</span>(<span class="kw">grouped_mean2</span>(mtcars, disp, cyl, am))
<span class="co">#&gt; [1] &quot;cyl&quot;      &quot;am&quot;       &quot;avg_disp&quot;</span></code></pre>
<p>Regarding the grouping variables, this is a case where explictly quoting and unquoting <code>...</code> pays off because we need to change the names of the list of quoted dots:</p>
<ul>
<li>Give default names to quoted dots with <code>.named = TRUE</code>.</li>
<li>Prepend the names of the list with a prefix.</li>
<li>Unquote-splice the list of quoted arguments as usual.</li>
</ul>
<pre class="sourceCode r"><code class="sourceCode r">grouped_mean2 &lt;-<span class="st"> </span><span class="cf">function</span>(.data, .summary_var, ...) {
  summary_var &lt;-<span class="st"> </span><span class="kw">enquo</span>(.summary_var)

  <span class="co"># Quote the dots with default names</span>
  group_vars &lt;-<span class="st"> </span><span class="kw">enquos</span>(..., <span class="dt">.named =</span> <span class="ot">TRUE</span>)

  summary_nm &lt;-<span class="st"> </span><span class="kw">quo_name</span>(summary_var)
  summary_nm &lt;-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">&quot;avg_&quot;</span>, summary_nm)

  <span class="co"># Modify the names of the list of quoted dots</span>
  <span class="kw">names</span>(group_vars) &lt;-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">&quot;groups_&quot;</span>, <span class="kw">names</span>(group_vars))

  .data <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">group_by</span>(<span class="op">!!!</span>group_vars) <span class="op">%&gt;%</span><span class="st">  </span><span class="co"># Unquote-splice as usual</span>
<span class="st">    </span><span class="kw">summarise</span>(<span class="op">!!</span>summary_nm <span class="op">:</span><span class="er">=</span><span class="st"> </span><span class="kw">mean</span>(<span class="op">!!</span>summary_var))
}

<span class="kw">grouped_mean2</span>(mtcars, disp, cyl, am)
<span class="co">#&gt; # A tibble: 6 x 3</span>
<span class="co">#&gt; # Groups:   groups_cyl [3]</span>
<span class="co">#&gt;   groups_cyl groups_am avg_disp</span>
<span class="co">#&gt;        &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;</span>
<span class="co">#&gt; 1          4         0    136. </span>
<span class="co">#&gt; 2          4         1     93.6</span>
<span class="co">#&gt; 3          6         0    205. </span>
<span class="co">#&gt; 4          6         1    155  </span>
<span class="co">#&gt; 5          8         0    358. </span>
<span class="co">#&gt; # … with 1 more row</span>

<span class="kw">names</span>(<span class="kw">grouped_mean2</span>(mtcars, disp, cyl, am))
<span class="co">#&gt; [1] &quot;groups_cyl&quot; &quot;groups_am&quot;  &quot;avg_disp&quot;</span></code></pre>
</div>
</div>
<div id="modifying-quoted-expressions" class="section level2">
<h2><span class="header-section-number">6.2</span> Modifying quoted expressions</h2>
<p>The quote-and-unquote pattern is a powerful and versatile technique. In this section we’ll use it for modifying quoted arguments.</p>
<p>In <a href="multiple.html#multiple">dealing with multiple arguments</a>, we have created a version of <code>grouped_mean()</code> that takes multiple grouping variables. Say we would like to take multiple summary variables instead. We could start by replacing <code>summary_var</code> with the <code>...</code> argument:</p>
<pre class="sourceCode r"><code class="sourceCode r">grouped_mean3 &lt;-<span class="st"> </span><span class="cf">function</span>(.data, .group_var, ...) {
  group_var &lt;-<span class="st"> </span><span class="kw">enquo</span>(.group_var)
  summary_vars &lt;-<span class="st"> </span><span class="kw">enquos</span>(..., <span class="dt">.named =</span> <span class="ot">TRUE</span>)

  .data <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">group_by</span>(<span class="op">!!</span>group_var) <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">summarise</span>(<span class="op">!!!</span>summary_vars)  <span class="co"># How do we take the mean?</span>
}</code></pre>
<p>The quoting part is easy. But how do we go about taking the average of each argument before passing them on to <code>summarise()</code>? We’ll have to modify the list of summary variables.</p>
<div id="expanding-quoted-expressions-with-expr" class="section level3">
<h3><span class="header-section-number">6.2.1</span> Expanding quoted expressions with <code>expr()</code></h3>
<p>Quoting and unquoting is an effective technique for modifying quoted expressions. But we’ll need to add one more function to our toolbox to work around the lack of unquoting support in <code>quote()</code>.</p>
<p>As we saw, the fundamental quoting function in R is <code>quote()</code>. All it does is return its quoted argument:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">quote</span>(<span class="kw">mean</span>(mass))
<span class="co">#&gt; mean(mass)</span></code></pre>
<p><code>quote()</code> does not support quasiquotation but tidy eval provides a variant that does. With <code>expr()</code>, you can quote expressions with full unquoting support:</p>
<pre class="sourceCode r"><code class="sourceCode r">vars &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="kw">quote</span>(mass), <span class="kw">quote</span>(height))

<span class="kw">expr</span>(<span class="kw">mean</span>(<span class="op">!!</span>vars[[<span class="dv">1</span>]]))
<span class="co">#&gt; mean(mass)</span>

<span class="kw">expr</span>(<span class="kw">group_by</span>(<span class="op">!!!</span>vars))
<span class="co">#&gt; group_by(mass, height)</span></code></pre>
<p>Note what just happened: by quoting-and-unquoting, we have expanded existing quoted expressions! This is the key to modifying expressions before passing them on to other quoting functions. For instance we could loop over the summary variables and unquote each of them in a <code>mean()</code> expression:</p>
<pre class="sourceCode r"><code class="sourceCode r">purrr<span class="op">::</span><span class="kw">map</span>(vars, <span class="cf">function</span>(var) <span class="kw">expr</span>(<span class="kw">mean</span>(<span class="op">!!</span>var, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)))
<span class="co">#&gt; [[1]]</span>
<span class="co">#&gt; mean(mass, na.rm = TRUE)</span>
<span class="co">#&gt; </span>
<span class="co">#&gt; [[2]]</span>
<span class="co">#&gt; mean(height, na.rm = TRUE)</span></code></pre>
<p>Let’s fix <code>grouped_mean3()</code> using this pattern:</p>
<pre class="sourceCode r"><code class="sourceCode r">grouped_mean3 &lt;-<span class="st"> </span><span class="cf">function</span>(.data, .group_var, ...) {
  group_var &lt;-<span class="st"> </span><span class="kw">enquo</span>(.group_var)
  summary_vars &lt;-<span class="st"> </span><span class="kw">enquos</span>(..., <span class="dt">.named =</span> <span class="ot">TRUE</span>)

  <span class="co"># Wrap the summary variables with mean()</span>
  summary_vars &lt;-<span class="st"> </span>purrr<span class="op">::</span><span class="kw">map</span>(summary_vars, <span class="cf">function</span>(var) {
    <span class="kw">expr</span>(<span class="kw">mean</span>(<span class="op">!!</span>var, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>))
  })

  <span class="co"># Prefix the names with `avg_`</span>
  <span class="kw">names</span>(summary_vars) &lt;-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">&quot;avg_&quot;</span>, <span class="kw">names</span>(summary_vars))

  .data <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">group_by</span>(<span class="op">!!</span>group_var) <span class="op">%&gt;%</span>
<span class="st">    </span><span class="kw">summarise</span>(<span class="op">!!!</span>summary_vars)
}</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">grouped_mean3</span>(starwars, species, height)
<span class="co">#&gt; # A tibble: 38 x 2</span>
<span class="co">#&gt;   species  avg_height</span>
<span class="co">#&gt;   &lt;chr&gt;         &lt;dbl&gt;</span>
<span class="co">#&gt; 1 &lt;NA&gt;            160</span>
<span class="co">#&gt; 2 Aleena           79</span>
<span class="co">#&gt; 3 Besalisk        198</span>
<span class="co">#&gt; 4 Cerean          198</span>
<span class="co">#&gt; 5 Chagrian        196</span>
<span class="co">#&gt; # … with 33 more rows</span>

<span class="kw">grouped_mean3</span>(starwars, species, height, mass)
<span class="co">#&gt; # A tibble: 38 x 3</span>
<span class="co">#&gt;   species  avg_height avg_mass</span>
<span class="co">#&gt;   &lt;chr&gt;         &lt;dbl&gt;    &lt;dbl&gt;</span>
<span class="co">#&gt; 1 &lt;NA&gt;            160       48</span>
<span class="co">#&gt; 2 Aleena           79       15</span>
<span class="co">#&gt; 3 Besalisk        198      102</span>
<span class="co">#&gt; 4 Cerean          198       82</span>
<span class="co">#&gt; 5 Chagrian        196      NaN</span>
<span class="co">#&gt; # … with 33 more rows</span></code></pre>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="multiple.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="glossary.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"google": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/ShixiangWang/tidyeval/edit/master/modify.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "section"
}
});
});
</script>

</body>

</html>
